package com.jeejam.product.service.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jeejam.framework.base.pojo.TreePager;
import com.jeejam.framework.base.service.impl.BaseServiceImpl;
import com.jeejam.product.dao.FilterMapper;
import com.jeejam.product.pojo.Filter;
import com.jeejam.product.service.FilterService;

@Service
public class FilterServiceImpl extends BaseServiceImpl implements FilterService{
	
	private FilterMapper filterMapper;
	
	@Resource
	public void setFilterMapper(FilterMapper filterMapper) {
		this.filterMapper = filterMapper;
		this.baseMapper = filterMapper;
	}

	@Override
	public List<Filter> selectRoot() {
		return filterMapper.selectRoot();
	}

	@Override
	public List<Filter> selectRootWithoutChildren() {
		return filterMapper.selectRootWithoutChildren();
	}

	@Override
	public List<Filter> selectRootByPage(int page, int pageSize) {
		Map<String, Serializable> params = new HashMap<String, Serializable>();
		params.put("begin", (page-1)*pageSize);
		params.put("pageSize", pageSize);
		return filterMapper.selectRootByPage(params);
	}

	@Override
	public List<Filter> selectChildren(int id) {
		return filterMapper.selectChildren(id);
	}

	@Override
	public TreePager getTreePager(int currentPage, int pageSize) {
		TreePager pager = new TreePager();
		int totalGroupNum = filterMapper.countsWithRoot();
		int totalNum = baseMapper.counts();
		int totalPage = totalGroupNum % pageSize == 0 ? totalGroupNum / pageSize : totalGroupNum / pageSize + 1;
		pager.setTotalGroupNum(totalGroupNum);
		pager.setTotalNum(totalNum);
		pager.setTotalPage(totalPage);
		pager.setCurrentPage(currentPage);
		pager.setPageSize(pageSize);
		return pager;
	}

	@Override
	public int countsWithRoot() {
		return filterMapper.countsWithRoot();
	}

	@Override
	public void deleteWithChildren(int id) {
		filterMapper.deleteWithChildren(id);
	}

}
